package com.smart.manager.system.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.smart.common.entities.ajax.AjaxResult;
import com.smart.common.entities.constant.ArcoTips;
import com.smart.common.entities.constant.SmartConstant;
import com.smart.common.entities.core.Page;
import com.smart.manager.system.entities.body.RSystemDict;
import com.smart.manager.system.entities.models.SystemDict;
import com.smart.manager.system.entities.models.query.QSystemDict;
import com.smart.manager.system.repository.DictRepository;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotEmpty;
import java.util.List;


/**
 * @author Administrator
 */

@Api(tags = "数据字典")
@RestController
@RequestMapping(SmartConstant.MANAGER_API_PREFIX + "system/dict")
@RequiredArgsConstructor
public class DictApi {

    private final DictRepository dictRepository;

    @GetMapping(value = "pageList")
    @ApiOperation(value = "查询字典")
    @SaCheckPermission("system:dict:pageList")
    public AjaxResult<Page<SystemDict>> pageList(@Validated  RSystemDict.SearchBody body) {
        Page<SystemDict> result = dictRepository.pageList(body);
        return AjaxResult.data(result);
    }


    @PostMapping(value = "add")
    @ApiOperation(value = "新增字典")
    @SaCheckPermission("system:dict:add")
    public AjaxResult<String> add(@RequestBody @Validated  RSystemDict.EditBody body) {
        dictRepository.add(body);
        return AjaxResult.success();
    }


    @PutMapping(value = "update/{id}")
    @ApiOperation(value = "修改字典")
    @SaCheckPermission("system:dict:update")
    public AjaxResult<String> update(@PathVariable Long id, @RequestBody @Validated  RSystemDict.EditBody body) {
        dictRepository.update(id, body);
        return AjaxResult.success();
    }


    @DeleteMapping(value = "delete")
    @ApiOperation(value = "删除字典")
    @SaCheckPermission("system:dict:delete")
    public AjaxResult<Integer> delete(@RequestBody @NotEmpty(message = ArcoTips.PK_EMPTY) List<Long> ids) {
        int x = dictRepository.delByIds(ids);
        return AjaxResult.delSuccess(x);
    }


    @PostMapping("download")
    @SaCheckPermission("system:dict:download")
    @ApiOperation(value = "下载字典")
    public AjaxResult<String> downLoad() {
        List<SystemDict> list =  new QSystemDict().findList();
        StringBuffer result = new StringBuffer();
        result.append(" export class DictOptions {  \n\n");
        String tpl = " \n // %s  \n  static %s = `"+SmartConstant.MANAGER_API_PREFIX+"system/support/dict/%s`  \n";
        list.forEach(x -> {
            String format = String.format(tpl, x.getTitle(), x.getCode(), x.getCode());
            result.append(format);
        });
        result.append("\n\n }");
        String s = result.toString();
        return AjaxResult.data(s);
    }

}
